package com.dnbiz.api.sdk.util;


/**
 * <pre>
 * <b>《各种URLPath路径获得方式》</b>;
 * 一、JSP或Servlet中获得相对路径，绝对路径的方法
 *   1.URI路径：getRequestURIPath(request)
 *   2.真实路径：getRealPath(request)
 *   3.真实路径父目录：getRealPathParent(request)
 *   4.HTTP站点全目录：getHttpWebFullPath(request)
 *   5.HTTP站点全目录：getHttpWebFullPath(request, childPath)
 *   
 * 二、java 的Class中获得相对路径，绝对路径的方法
 *   1.Class包路径：getClassesPackagePath(Class<?> clazz)
 *   2.Java编译Class路径：getClassesPath()
 *   3.WEB-INF路径：getWEBINFPath()
 *   4.WebRoot路径：getWebRootPath()
 *   5.Class下的文件或目录路径：getClassesFilePath(String fileName)
 *   6.WEB-INF下文件或目录路径：getWEBINFFilePath(String fileName)
 *   7.WebRoot下文件或目录路径：getWebRootFilePath(String fileName)
 * </pre>
 * @author HuangFeng(Mar 31, 2010)
 * @version 1.0
 * @copyright 杭州三六五电子商务股份有限公司
 */
public class PathUtil {

	private static final String WEB_INF = "WEB-INF";
	private static final String USER_DIR = "user.dir";
	
	public static String getSystemUserDir()
	{
		return System.getProperty(USER_DIR);
	}
	
	/**
	 * <pre>
	 * Class包完整路径
	 * [/E:/workspace/365BTOCsp10/WebRoot/WEB-INF/classes/com/net365/b2c/util/]
	 * @see clazz.getResource("").getPath();
	 * </pre>
	 * @author HuangFeng(Mar 31, 2010)
	 * @param clazz Class<?>
	 * @return Class包完整路径
	 */
	public static String getClassesPackagePath(Class<?> clazz)
	{
		return replace(clazz.getResource("").getPath());
	}
	
	/**
	 * <pre>
	 * Class包下完整文件路径
	 * [/E:/workspace/365BTOCsp10/WebRoot/WEB-INF/classes/com/net365/b2c/util/package.html]
	 * @see clazz.getResource("").getPath();
	 * </pre>
	 * @author HuangFeng(Mar 31, 2010)
	 * @param clazz Class<?>
	 * @param fileName 文件名称
	 * @return Class包完整路径
	 */
	public static String getClassesPackagePath(Class<?> clazz, String fileName)
	{
		return (replace(clazz.getResource("").getPath()) + fileName);
	}
	
	/**
	 * <pre>
	 * Java编译Class路径
	 * [/E:/workspace/365BTOCsp10/WebRoot/WEB-INF/classes/]
	 * @see class.getResource("/").getPath()
	 * </pre>
	 * @author HuangFeng(Mar 31, 2010)
	 * @return Java编译Class路径
	 */
	public static String getClassesPath()
	{
		return replace(PathUtil.class.getResource("/").getPath());
	}
	
	/**
	 * <pre>
	 * Class下的文件或目录路径
	 * [/E:/workspace/365BTOCsp10/WebRoot/WEB-INF/classes/config.xml]
	 * @see getClassesPath() + fileName;
	 * </pre>
	 * @param fileName 文件或目录
	 * @author HuangFeng(Mar 31, 2010)
	 * @return Class下的文件或目录路径
	 */
	public static String getClassesFilePath(String fileName)
	{
		return (getClassesPath() + fileName);
	}

	/**
	 * <pre>
	 * WEB-INF路径
	 * [/E:/workspace/365BTOCsp10/WebRoot/WEB-INF/]
	 * </pre>
	 * @author HuangFeng(Mar 31, 2010)
	 * @return WEB-INF路径
	 */
	public static String getWEBINFPath()
	{
		String path = getClassesPath();
		return path.substring(0, (path.indexOf(WEB_INF) + WEB_INF.length() + 1));
	}

	/**
	 * <pre>
	 * WEB-INF下文件或目录路径
	 * [/E:/workspace/365BTOCsp10/WebRoot/WEB-INF/config.xml]
	 * </pre>
	 * @param fileName 文件或目录
	 * @author HuangFeng(Mar 31, 2010)
	 * @return WEB-INF下文件或目录路径
	 */
	public static String getWEBINFFilePath(String fileName)
	{
		return getWEBINFPath() + fileName;
	}
	
	/**
	 * <pre>
	 * WebRoot路径
	 * [/E:/workspace/365BTOCsp10/WebRoot/]
	 * </pre>
	 * @author HuangFeng(Mar 31, 2010)
	 * @return WEB-INF路径
	 */
	public static String getWebRootPath()
	{
		String path = getWEBINFPath();
		return path.substring(0, path.indexOf(WEB_INF));
	}
	
	/**
	 * <pre>
	 * WebRoot下文件或目录路径
	 * [/E:/workspace/365BTOCsp10/WebRoot/uploads/]
	 * </pre>
	 * @param fileName 文件或目录
	 * @author HuangFeng(Mar 31, 2010)
	 * @return WebRoot下文件或目录路径
	 */
	public static String getWebRootFilePath(String fileName)
	{
		return getWebRootPath() + fileName;
	}
//
//	/**
//	 * <pre>
//	 * URI路径
//	 * [/html/system/system/main_set_urlrewrite.jsp]
//	 * @see request.getRequestURI();
//	 * </pre>
//	 * @author HuangFeng(Mar 31, 2010)
//	 * @param request HttpServletRequest
//	 * @return URI路径
//	 */
//	public static String getRequestURIPath(HttpServletRequest request)
//	{
//		return replace(request.getRequestURI());
//	}
//
//	/**
//	 * <pre>
//	 * 真实路径
//	 * [E:\workspace\365BTOCsp10\WebRoot\html\system\system\main_set_urlrewrite.jsp]
//	 * @see request.getSession().getServletContext().getRealPath(request.getRequestURI());
//	 * </pre>
//	 * @author HuangFeng(Mar 31, 2010)
//	 * @param request HttpServletRequest
//	 * @return 真实路径
//	 */
//	public static String getRealPath(HttpServletRequest request)
//	{
//		return replace(request.getSession().getServletContext().getRealPath(request.getRequestURI()));
//	}
//	
//	/**
//	 * <pre>
//	 * 真实路径父目录
//	 * [E:\workspace\365BTOCsp10\WebRoot\html\system\system\]
//	 * @see new File(getRealPath(request)).getParent();
//	 * </pre>
//	 * @author HuangFeng(Mar 31, 2010)
//	 * @param request HttpServletRequest
//	 * @return 真实路径父目录
//	 */
//	public static String getRealPathParent(HttpServletRequest request)
//	{
//		return new java.io.File(getRealPath(request)).getParent();
//	}
//	
//	/**
//	 * <pre>
//	 * HTTP站点全目录
//	 * [http://localhost:8080/365BTOC/]
//	 * @see (request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/");
//	 * </pre>
//	 * @author HuangFeng(Mar 31, 2010)
//	 * @param request HttpServletRequest
//	 * @return HTTP站点全目录
//	 */
//	public static String getHttpWebFullPath(HttpServletRequest request)
//	{
//		String path = request.getContextPath();
//		return (request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/");
//	}
//	
//	/**
//	 * <pre>
//	 * HTTP站点全目录
//	 * [http://localhost:8080/365BTOC/club]
//	 * @see (request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/" + childPath);
//	 * </pre>
//	 * @author HuangFeng(Mar 31, 2010)
//	 * @param request HttpServletRequest
//	 * @param childPath 子Http路径
//	 * @return HTTP站点全目录
//	 */
//	public static String getHttpWebFullPath(HttpServletRequest request, String childPath)
//	{
//		String path = request.getContextPath();
//		return (request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/" + childPath);
//	}
	
	static String replace(String path)
	{
		path = path.replace("%20", " ");
		if(path.startsWith("/") && !System.getProperty("os.name").toLowerCase().equals("linux"))
		{
			path = path.substring(1, path.length());
		}
		return path;
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println("getClassesFilePath:"  + PathUtil.getClassesFilePath("config.xml"));
		System.out.println("getClassesPackagePath:"  + PathUtil.getClassesPackagePath(PathUtil.class));
		System.out.println("getClassesPath:"  + PathUtil.getClassesPath());
		System.out.println("getWEBINFFilePath:"  + PathUtil.getWEBINFFilePath("config.xml"));
		System.out.println("getWEBINFPath:"  + PathUtil.getWEBINFPath());
		System.out.println("getWebRootFilePath:"  + PathUtil.getWebRootFilePath("config.xml"));
		System.out.println("getWebRootPath:"  + PathUtil.getWebRootPath());
	}

	/**
	 * 获取相对路径
	 * @param path　目标文件或目录
	 * @param dirpath　相对的根节点
	 * @return
	 */
	public static String getOpposite(String path, String dirpath) {
		if(path.startsWith(dirpath)){
//			System.out.println("path:"+path);
//			System.out.println("path.length:"+path.length());
//			System.out.println("dirpath:"+dirpath);
//			System.out.println("dirpath.length:"+dirpath.length());
			return path.substring(dirpath.length());
		}
		return StringPool.BLANK;
	}
}
